﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;

namespace IHISCETeim.BLL
{
    public class UMacKeepController : BaseController
    {
        /// <summary>
        /// 新增机器保养信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(UMacKeepInfo obj)
        {
            int r = UMacKeepDAL.Add(obj);
            if (r > 0)
                AddKeepSMS(obj, 50);
            return r;
        }
        public static bool Delete(string SerialNumber)
        {
            return UMacKeepDAL.Delete(SerialNumber);
        }
        public static UMacKeepInfo GetBySerialNumber(string SerialNumber)
        {
            UMacKeepInfo obj = CBO.FillObject<UMacKeepInfo>(UMacKeepDAL.GetBySerialNumber(SerialNumber));
            if (obj == null)
                obj = new UMacKeepInfo();
            return obj;
        }
        public static DataTableCollection GetPagination(string sqlwhere, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return UMacKeepDAL.GetPagination(sqlwhere, PageIndex, PageSize, Sort, ref TotalCount);
        }
        public static bool Update(UMacKeepInfo obj)
        {
            //List<UMacKeepAlarmInfo> listUAlarm = UMacKeepAlarm_GetBySerialNumber(obj.SerialNumber);
            //foreach (UMacKeepAlarmInfo itm in listUAlarm)
            //{
            //    if (itm.AlarmDate > DateTime.Now || itm.State==false)
            //    {
            //        UMacKeepAlarm_Delete(itm.UMKAID);
            //    }
            //}
            //AddKeepSMS(obj);
            return UMacKeepDAL.Update(obj);
        }
        public static bool Update(UWarrantyCardInfo newobj, UWarrantyCardInfo oldobj)
        {

            UMacKeepInfo UKObj = new UMacKeepInfo();
            UKObj = GetBySerialNumber(oldobj.SerialNumber);
            UKObj.SerialNumber = newobj.SerialNumber.ToUpper().Trim();
            UKObj.IsWarrantyCard = true;
            UKObj.IntoDate = newobj.IntoDate;
            UKObj.IntoHours = newobj.IntoHours;
            UKObj.CustomerName = newobj.CustomerName;
            UKObj.CustomerMobile = newobj.CustomerMobile;
            UKObj.CustomerAddress = newobj.CustomerAddress;
            UKObj.AgentName = newobj.AgentName;
            UKObj.AgentTel = newobj.AgentTel;

            //UMacKeepController.Update(UKObj);

            return Update(UKObj);
        }
        public static int UMacKeepDetail_Add(UMacKeepDetailInfo obj)
        {
            /*处理下次保养时间短信提醒*/
            int NextKeepHours = 0;
            if (obj.KeepHours == 50)
                NextKeepHours = 250;
            else
                NextKeepHours = obj.KeepHours + 250;

            UMacKeepInfo objUKeep = GetBySerialNumber(obj.SerialNumber);

            int returnv = UMacKeepDAL.UMacKeepDetail_Add(obj);
            if (returnv > 0)
                AddKeepSMS(objUKeep, NextKeepHours);
            return returnv;

        }
        public static bool UMacKeepDetail_Delete(int UMKDID)
        {
            return UMacKeepDAL.UMacKeepDetail_Delete(UMKDID);
        }

        public static UMacKeepDetailInfo UMacKeepDetail_GetByID(int UMKDID)
        {
            return CBO.FillObject<UMacKeepDetailInfo>(UMacKeepDAL.UMacKeepDetail_GetByID(UMKDID));
        }
        public static bool UMacKeepDetail_Update(UMacKeepDetailInfo obj)
        {
            return UMacKeepDAL.UMacKeepDetail_Update(obj);
        }


        public static List<UMacKeepDetailInfo> UMacKeepDetail_GetBySerialNumber(string SerialNumber)
        {
            return CBO.FillCollection<UMacKeepDetailInfo>(UMacKeepDAL.UMacKeepDetail_GetBySerialNumber(SerialNumber));
        }
        /// <summary>
        /// 获取代理商机器数量
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAgentMacNum()
        {
            return UMacKeepDAL.GetAgentMacNum();
        }
        #region 短信相关
        public static int UMacKeepAlarm_Add(UMacKeepAlarmInfo obj)
        {
            return 1;//2011-11-14先暂定发短信
            return UMacKeepDAL.UMacKeepAlarm_Add(obj);
        }
        public static bool UMacKeepAlarm_Delete(int UMKAID)
        {
            return UMacKeepDAL.UMacKeepAlarm_Delete(UMKAID);
        }
        public static DataTable UMacKeepAlarm_GetALL()
        {
            return UMacKeepDAL.UMacKeepAlarm_GetALL();
        }
        /// <summary>
        /// 获取机器的短信发送记录
        /// </summary>
        /// <param name="SerialNumber"></param>
        /// <returns></returns>
        public static List<UMacKeepAlarmInfo> UMacKeepAlarm_GetBySerialNumber(string SerialNumber)
        {
            List<UMacKeepAlarmInfo> list = CBO.FillCollection<UMacKeepAlarmInfo>(UMacKeepDAL.UMacKeepAlarm_GetBySerialNumber(SerialNumber));
            if (list == null)
                return new List<UMacKeepAlarmInfo>();
            return list;
        }

        public static bool UMacKeepAlarm_UpdateSendSMS(int UMKAID)
        {
            return UMacKeepDAL.UMacKeepAlarm_UpdateSendSMS(UMKAID);
        }
        public static List<UMacKeepConfigInfo> UMacKeepConfig_GetALL()
        {
            return CBO.FillCollection<UMacKeepConfigInfo>(UMacKeepDAL.UMacKeepConfig_GetALL());
        }
        public static bool UMacKeepConfig_Update(UMacKeepConfigInfo obj)
        {
            return UMacKeepDAL.UMacKeepConfig_Update(obj);
        }
        /// <summary>
        /// 添加机器保养短信通知
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="NextKeepHours">下次要提醒保养的时间</param>
        private static void AddKeepSMS(UMacKeepInfo obj, int NextKeepHours)
        {
            List<UMacKeepConfigInfo> listConfig = UMacKeepConfig_GetALL();
            try
            {
                UMacKeepConfigInfo item = listConfig.First(e => e.KeepHours == NextKeepHours);
                UMacKeepAlarmInfo UAObj = new UMacKeepAlarmInfo();
                UAObj.SerialNumber = obj.SerialNumber;
                UAObj.KeepHours = item.KeepHours;
                UAObj.AlarmDate = obj.IntoDate.AddDays(item.KeepDay);
                UAObj.AlarmSMSContent = GetSMSContent(obj, item.Tkey);
                UMacKeepAlarm_Add(UAObj);
            }
            catch
            {
                return;
            }


        }
        /// <summary>
        /// 获取短信内容
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="Tkey"></param>
        /// <returns></returns>
        private static string GetSMSContent(UMacKeepInfo obj, string Tkey)
        {
            USMSTempleteInfo smsobj = USMSTempleteController.GetByKey(Tkey);
            string smscontent = smsobj.SMSContent.Replace(",", "，");

            // string smscontent = SMSContent;
            smscontent = smscontent.Replace("[客户名称]", obj.CustomerName);
            smscontent = smscontent.Replace("[客户手机]", obj.CustomerMobile);
            smscontent = smscontent.Replace("[交机日期]", obj.IntoDate.ToShortDateString());
            smscontent = smscontent.Replace("[机号]", obj.SerialNumber);
            smscontent = smscontent.Replace("[经销商]", obj.AgentName);
            smscontent = smscontent.Replace("[服务电话]", obj.AgentTel);
            return smscontent;
        }
        #endregion

        /// <summary>
        /// 定期服务实施准确率
        /// </summary>
        /// <param name="f"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static DataTable GetDQHWSSZQL(string f, string t)
        {
            return UMacKeepDAL.GetDQHWSSZQL(f, t);
        }
        public static DataTable GetZQL(string f, string t)
        {
            return UMacKeepDAL.GetZQL(f, t);
        }
    }
}
